//
//  JWTChangePWDViewController.m
//  JWT
//
//  Created by ndm on 17/2/3.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTChangePWDViewController.h"
#import "JWTTextField.h"

@interface JWTChangePWDViewController ()<UITextFieldDelegate>
{
    //原密码
    JWTTextField *_oldPassWordTextField;
    //新密码
    JWTTextField *_newPassWordTextField;
    //确认密码
    JWTTextField *_checkPassWordTextField;
    
    
}

@property (nonatomic,strong) UIButton *confPWDBtn;
@end

@implementation JWTChangePWDViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithNavTitle:@"修改密码"];
    
    [self initWithAppearance];
}



-(void) initWithAppearance{
    CGFloat margin  = 10;
    _oldPassWordTextField = [[JWTTextField alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH*0.15, self.view.top+Cell_Margin*4, UISCREEN_WIDTH-2*UISCREEN_WIDTH*0.15, 50)];
    _oldPassWordTextField.layer.borderWidth = 0.5f;
    _oldPassWordTextField.layer.cornerRadius = _oldPassWordTextField.height * 0.5;
    _oldPassWordTextField.layer.borderColor = RGB_LightGray.CGColor;
    _oldPassWordTextField.placeholder = @"请输入原密码";
    _oldPassWordTextField.secureTextEntry = YES;
    _oldPassWordTextField.delegate = self;
    _oldPassWordTextField.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:_oldPassWordTextField];
    
    UIImageView *oldPassWordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Cell_Margin, 8, 30, 30)];
    [oldPassWordImageView setImage:[UIImage imageNamed:@"PassWord"]];
    oldPassWordImageView.contentMode = UIViewContentModeScaleAspectFit;
    oldPassWordImageView.layer.masksToBounds = YES;
    _oldPassWordTextField.leftViewMode=UITextFieldViewModeAlways;
    _oldPassWordTextField.leftView.frame = CGRectMake(Cell_Margin, 8, 34, 34);
    _oldPassWordTextField.leftView = oldPassWordImageView;
    
    
    _newPassWordTextField = [[JWTTextField alloc] initWithFrame:CGRectMake(_oldPassWordTextField.left, _oldPassWordTextField.bottom+Cell_Margin,_oldPassWordTextField.width, _oldPassWordTextField.height)];
    _newPassWordTextField.layer.borderWidth = 0.5f;
    _newPassWordTextField.layer.cornerRadius = _newPassWordTextField.height * 0.5;
    _newPassWordTextField.layer.borderColor = RGB_LightGray.CGColor;
    _newPassWordTextField.placeholder = @"请输入新密码";
    _newPassWordTextField.secureTextEntry = YES;
    _newPassWordTextField.delegate = self;
    _newPassWordTextField.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:_newPassWordTextField];
    
    UIImageView *newPassWordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Cell_Margin, 8, 30, 30)];
    [newPassWordImageView setImage:[UIImage imageNamed:@"PassWord"]];
    newPassWordImageView.contentMode = UIViewContentModeScaleAspectFit;
    newPassWordImageView.layer.masksToBounds = YES;
    _newPassWordTextField.leftViewMode=UITextFieldViewModeAlways;
    _newPassWordTextField.leftView.frame = CGRectMake(Cell_Margin, 8, 34, 34);
    _newPassWordTextField.leftView = newPassWordImageView;
    
    //确认密码
    _checkPassWordTextField = [[JWTTextField alloc] initWithFrame:CGRectMake(_newPassWordTextField.left, _newPassWordTextField.bottom+Cell_Margin,_newPassWordTextField.width, _newPassWordTextField.height)];
    _checkPassWordTextField.layer.borderWidth = 0.5f;
    _checkPassWordTextField.layer.cornerRadius = _checkPassWordTextField.height * 0.5;
    _checkPassWordTextField.layer.borderColor = RGB_LightGray.CGColor;
    _checkPassWordTextField.placeholder = @"请输入确认密码";
    _checkPassWordTextField.secureTextEntry = YES;
    _checkPassWordTextField.delegate = self;
    _checkPassWordTextField.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:_checkPassWordTextField];
    
    UIImageView *checkPassWordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Cell_Margin, 8, 30, 30)];
    [checkPassWordImageView setImage:[UIImage imageNamed:@"PassWord"]];
    checkPassWordImageView.contentMode = UIViewContentModeScaleAspectFit;
    checkPassWordImageView.layer.masksToBounds = YES;
    _checkPassWordTextField.leftViewMode=UITextFieldViewModeAlways;
    _checkPassWordTextField.leftView.frame = CGRectMake(Cell_Margin, 8, 34, 34);
    _checkPassWordTextField.leftView = checkPassWordImageView;
    
    //注册
    _confPWDBtn = [[UIButton alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH*0.25, _checkPassWordTextField.bottom+margin*2, UISCREEN_WIDTH*0.5, 50)];
    _confPWDBtn.layer.borderWidth = 0.5f;
    _confPWDBtn.layer.cornerRadius = _confPWDBtn.height * 0.5;
    _confPWDBtn.layer.borderColor = RGB_LightGray.CGColor;
    [_confPWDBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [_confPWDBtn setTitleColor:RGB_WhiteBackGround forState:UIControlStateNormal];
    [_confPWDBtn setBackgroundColor:HeaderBackground_Color];
    [self.view addSubview:_confPWDBtn];
    
    WS(weakSelf);
    [_confPWDBtn addButtonBlockControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [weakSelf changePWDCommit];
    }];
}

/**确认修改密码回调事件－－*/

-(void) changePWDCommit{
    [self.view endEditing:YES];
    
    //判断新旧密码是否一致
    if ([_newPassWordTextField.text isEqualToString:@""]||[_checkPassWordTextField.text isEqualToString:@""]||[_oldPassWordTextField.text isEqualToString:@""]) {
        [HUD showToast:@"密码框不能为空,请输入"];
    }else if ([_newPassWordTextField.text length] < 6 || [_checkPassWordTextField.text length] < 6 || [_newPassWordTextField.text length]< 6){
        [HUD showToast:@"密码位数不小于6位"];
    }
    
    else if (![_newPassWordTextField.text isEqualToString:_checkPassWordTextField.text]) {
        [HUD showToast:@"两次密码输入不一致,请重新确认"];
    }else{
        [HUD showUIBlockingIndicator];
        NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] updatePasswordWithActionUpdatePassword];
        
        NSDictionary *dict = @{@"userid":APPDELEGATE.userInfo.userid,@"password":[NSString md5:_newPassWordTextField.text],@"tokenid":APPDELEGATE.userInfo.tokenid};
        
        [AFNHttpsRequest requestWithUrl:urlString WithParam:dict WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
            [HUD hideUIBlockingIndicator];
            [self resignFirstResponseAction];
            if (model) {
                [HUD showToast:[model objectForKey:@"message"]];
                if ([[model objectForKey:@"code"] integerValue] == 200) {
                    NSLog(@"%@",model);
                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                    [userDefaults setObject:APPDELEGATE.userInfo.tel forKey:@"tel"];
                    [userDefaults setObject:@"" forKey:@"userName"];
                    [userDefaults setObject:@"" forKey:@"passWord"];
                    [userDefaults setObject:@"" forKey:@"tokenID"];
                    [userDefaults synchronize];
                    APPDELEGATE.isLogin = NO;
                    APPDELEGATE.userInfo = nil;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"Add_UserPhoneTextAction" object:nil];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }
            }
        }];
        
    }
}

-(void) resignFirstResponseAction{
    [_oldPassWordTextField resignFirstResponder];
    [_newPassWordTextField resignFirstResponder];
    [_checkPassWordTextField resignFirstResponder];
}

@end
